package benny_driving_ser.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;

import benny_driving_ser.dao.DriStateDao;
import benny_driving_ser.dao.impl.DriStateDaoImpl;
import benny_driving_ser.entity.SessionAttributeDriId;
import benny_driving_ser.util.SessionUtil;

public class DriStateAction extends BaseAction{
	
	private PrintWriter out;
	private DriStateDao dao;
	private int driState;
	
	public String queryStateById(){
		
		SessionAttributeDriId sad = SessionUtil.getInstance()
				.verifySessionIdForDri(request);
		
		String method = ServletActionContext.getRequest().getMethod();
		
		System.out.println("method:"+method);
		
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			
			if(sad!=null){
				System.out.println("driid:"+sad.getValue());
				dao = new DriStateDaoImpl();
				driState = dao.queryStateById(sad.getValue());
				
				System.out.println("driState:"+driState);
				out.print(driState);
				out.flush();
				out.close();
				
				return "success";
			}else{
				out.print(-1);
				out.flush();
				out.close();
			}
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		}
		
		
		
		return "fail";
	}


	public PrintWriter getOut() {
		return out;
	}


	public void setOut(PrintWriter out) {
		this.out = out;
	}


	public DriStateDao getDao() {
		return dao;
	}


	public void setDao(DriStateDao dao) {
		this.dao = dao;
	}


	public int getDriState() {
		return driState;
	}


	public void setDriState(int driState) {
		this.driState = driState;
	}
	
	
}
